var express = require('express');
var router = express.Router();
let knex = require('../common/getknex')();
/* GET home page. */
router.get('/', async (req, res, next) => {
  //await knex('data').insert
  let info_pri = await knex('data').orderBy('id', 'desc').limit(1);
  // let info_pri = await knex('data').select().where('id', 47);
  info_de = info_pri[0].info;
  let info_de_arr = info_de.split('\n')[0].split(';');
  let info_de_arr1 = info_de_arr.map((val) => {
    if (val !== '') {
      return val.replace(/.*=(.*)/, "$1")
    }
  })
  console.log(info_de_arr1);
  let info = {
    wind_scale: info_de_arr1[0] ? info_de_arr1[0] : '未获取到风力数据',
    direction: info_de_arr1[1] ? info_de_arr1[1] : '未获取到风向数据',
    temperature: info_de_arr1[3] ? info_de_arr1[3] : '未获取到气温数据',
    humidity: info_de_arr1[2] ? info_de_arr1[2] : '未获取到湿度数据'
  }
  // res.json(info);
  res.render('index', {
    title: '基于物联网的道路信息采集系统',
    direction: info.direction,
    wind_scale: info.wind_scale,
    temperature: info.temperature,
    humidity: info.humidity
  });
});

module.exports = router;
